package agu.json;

import org.json.JSONArray;
import org.json.JSONObject;

public interface $JSON<Key>
{
    <Obj extends $JSON<?>> Obj put(Key key, Object value);

    Object opt(Key key);

    default JSONObject2 optJSONObject2(Key key){
        Object value = opt(key);

        if(value instanceof JSONObject2) return (JSONObject2)value;

        if(value instanceof JSONObject){
            JSONObject2 value2 = new JSONObject2(value);
            put(key, value2);
            return value2;
        }

        return null;
    }

    default JSONArray2 optJSONArray2(Key key){
        Object value = opt(key);

        if(value instanceof JSONArray2) return (JSONArray2)value;

        if(value instanceof JSONArray){
            JSONArray2 value2 = new JSONArray2(value);
            put(key, value2);
            return value2;
        }

        return null;
    }
}
